<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function foo(name, age) {
      console.log(this, name, age)
    }

    foo.apply('aaa', ['why', 18])
    foo.apply('bbb', ['kobe', 30])

    // 1.给函数对象添加方法
    Function.prototype.hyapply = function (thisArg, otherArgs) {
      console.log(this) // 当前调用的函数对象

      thisArg = (thisArg === null || thisArg === undefined) ? window : Object(thisArg)
      // thisArg.fn = this
      Object.defineProperty(thisArg, 'fn', {
        enumerable: false,
        configurable: true,
        value: this
      })
      thisArg.fn(...otherArgs)
      delete thisArg.fn
    }
    foo.hyapply({ name: 'why' },['james',25])
    // foo.hyapply(123)
    // foo.hyapply(null)

    Function.prototype.hycall = function (thisArg, ...otherArgs) {
      thisArg = (thisArg === null || thisArg === undefined) ? window : Object(thisArg)
      Object.defineProperty(thisArg, 'fn', {
        enumerable: false,
        configurable: true,
        value: this
      })
      thisArg.fn(...otherArgs)
      delete thisArg.fn
    }
    foo.hycall({ name: 'why' },'james',25)

  </script>
</body>

</html>